#include "GLSceneFrameAndMaskDrawers.h"

#include "gl.h"

namespace catan
{

/*static*/ void GLSceneFrameAndMaskDrawers::drawOrthogonalFrameAroundClient(SDLCanvas& sdlCanvas)
{
	GLint oldMatrixMode;
	glGetIntegerv(GL_MATRIX_MODE, &oldMatrixMode);
	GLfloat oldColor[4];
	glGetFloatv(GL_CURRENT_COLOR, oldColor);
	    glMatrixMode(GL_MODELVIEW);
	    glPushMatrix();
		    glLoadIdentity();
			glColor3f(1.0f, 1.0f, 1.0f);
			float width = sdlCanvas.getClientUnitWidth();
			float height = sdlCanvas.getClientUnitHeight();
		    glBegin(GL_LINE_LOOP);
			    glVertex3f(width, height, 0.0f);
			    glVertex3f(width, 0.0f, 0.0f);
				glColor3f(1.0f, 0.0f, 0.0f);
			    glVertex3f(0.0f, 0.0f, 0.0f);
				glColor3f(1.0f, 1.0f, 1.0f);
			    glVertex3f(0.0f, height, 0.0f);
		    glEnd();
		glPopMatrix();
	glColor3f(oldColor[0], oldColor[1], oldColor[2]);
	glMatrixMode(oldMatrixMode);
}

/*static*/ void GLSceneFrameAndMaskDrawers::drawProjectionFrameAroundClient(SDLCanvas& sdlCanvas)
{
	GLint oldMatrixMode;
	glGetIntegerv(GL_MATRIX_MODE, &oldMatrixMode);
	GLfloat oldColor[4];
	glGetFloatv(GL_CURRENT_COLOR, oldColor);
	    glMatrixMode(GL_MODELVIEW);
	    glPushMatrix();
		    glLoadIdentity();
		    glTranslatef(0.0f, 0.0f, -1.0f);
			glColor3f(1.0f, 1.0f, 1.0f);
			float halfWidth = sdlCanvas.getClientUnitWidth()/2;
			float halfHeight = sdlCanvas.getClientUnitHeight()/2;
		    glBegin(GL_LINE_LOOP);
			    glVertex3f(halfWidth, halfHeight, 0.0f);
			    glVertex3f(halfWidth, -halfHeight, 0.0f);
				glColor3f(0.0f, 0.0f, 1.0f);
			    glVertex3f(-halfWidth, -halfHeight, 0.0f);
				glColor3f(1.0f, 1.0f, 1.0f);
			    glVertex3f(-halfWidth, halfHeight, 0.0f);
		    glEnd();
		glPopMatrix();
	glColor3f(oldColor[0], oldColor[1], oldColor[2]);
	glMatrixMode(oldMatrixMode);
}

/*static*/ void GLSceneFrameAndMaskDrawers::drawOrthogonalMaskAroundClient(SDLCanvas& sdlCanvas)
{
	GLint oldMatrixMode;
	glGetIntegerv(GL_MATRIX_MODE, &oldMatrixMode);
	GLfloat oldColor[4];
	glGetFloatv(GL_CURRENT_COLOR, oldColor);
	    glMatrixMode(GL_MODELVIEW);
	    glPushMatrix();
		    glLoadIdentity();
			glColor3f(0.0f, 0.0f, 0.0f);
			float innerWidth = sdlCanvas.getClientUnitWidth();
			float innerHeight = sdlCanvas.getClientUnitHeight();
			float xOffset = (sdlCanvas.getWindowUnitWidth() - sdlCanvas.getClientUnitWidth())/2;
			float yOffset = (sdlCanvas.getWindowUnitHeight() - sdlCanvas.getClientUnitHeight())/2;
		    glBegin(GL_QUADS);
				if (xOffset > 0.0f)
				{
					glVertex3f(0.0f, 0.0f, 0.0f);
					glVertex3f(-xOffset, 0.0f, 0.0f);
					glVertex3f(-xOffset, innerHeight, 0.0f);
					glVertex3f(0.0f, innerHeight, 0.0f);

					glVertex3f(innerWidth+xOffset, 0.0f, 0.0f);
					glVertex3f(innerWidth, 0.0f, 0.0f);
					glVertex3f(innerWidth, innerHeight, 0.0f);
					glVertex3f(innerWidth+xOffset, innerHeight, 0.0f);
				}
				else
				{
					glVertex3f(innerWidth, -yOffset, 0.0f);
					glVertex3f(0.0f, -yOffset, 0.0f);
					glVertex3f(0.0f, 0.0f, 0.0f);
					glVertex3f(innerWidth, 0.0f, 0.0f);

					glVertex3f(innerWidth, innerHeight, 0.0f);
					glVertex3f(0.0f, innerHeight, 0.0f);
					glVertex3f(0.0f, innerHeight+yOffset, 0.0f);
					glVertex3f(innerWidth, innerHeight+yOffset, 0.0f);
				}
		    glEnd();
		glPopMatrix();
	glColor3f(oldColor[0], oldColor[1], oldColor[2]);
	glMatrixMode(oldMatrixMode);
}

/*static*/ void GLSceneFrameAndMaskDrawers::drawOrthogonalMaskAroundMapArea(float fractionOffLeft, float fractionOffRight, float fractionOffBottom, float fractionOffTop, SDLCanvas& sdlCanvas)
{
	float oneMinusFractionOffRight = 1.0f - fractionOffRight;
	float oneMinusFractionOffBottom = 1.0f - fractionOffBottom;
	GLint oldMatrixMode;
	glGetIntegerv(GL_MATRIX_MODE, &oldMatrixMode);
	GLfloat oldColor[4];
	glGetFloatv(GL_CURRENT_COLOR, oldColor);
	    glMatrixMode(GL_MODELVIEW);
	    glPushMatrix();
		    glLoadIdentity();
			glColor3f(0.0f, 0.0f, 0.0f);
			float innerWidth = sdlCanvas.getClientUnitWidth();
			float innerHeight = sdlCanvas.getClientUnitHeight();
		    glBegin(GL_QUADS);
		    	glVertex3f(innerWidth, 0.0f, 0.0f);
		    	glVertex3f(innerWidth*fractionOffLeft, 0.0f, 0.0f);
				glVertex3f(innerWidth*fractionOffLeft, innerHeight*fractionOffTop, 0.0f);
				glVertex3f(innerWidth, innerHeight*fractionOffTop, 0.0f);
				
		    	glVertex3f(innerWidth*fractionOffLeft, 0.0f, 0.0f);
		    	glVertex3f(0.0f, 0.0f, 0.0f);
				glVertex3f(0.0f, innerHeight*oneMinusFractionOffBottom, 0.0f);
				glVertex3f(innerWidth*fractionOffLeft, innerHeight*oneMinusFractionOffBottom, 0.0f);
				
		    	glVertex3f(innerWidth*oneMinusFractionOffRight, innerHeight*oneMinusFractionOffBottom, 0.0f);
		    	glVertex3f(0.0f, innerHeight*oneMinusFractionOffBottom, 0.0f);
				glVertex3f(0.0f, innerHeight, 0.0f);
				glVertex3f(innerWidth*oneMinusFractionOffRight, innerHeight, 0.0f);
				
		    	glVertex3f(innerWidth, innerHeight*fractionOffTop, 0.0f);
		    	glVertex3f(innerWidth*oneMinusFractionOffRight, innerHeight*fractionOffTop, 0.0f);
				glVertex3f(innerWidth*oneMinusFractionOffRight, innerHeight, 0.0f);
				glVertex3f(innerWidth, innerHeight, 0.0f);
		    glEnd();
		glPopMatrix();
	glColor3f(oldColor[0], oldColor[1], oldColor[2]);
	glMatrixMode(oldMatrixMode);
}

/*private*/ GLSceneFrameAndMaskDrawers::GLSceneFrameAndMaskDrawers()
{
}

}